package cool.webstudy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cool.webstudy.admin.constant.enums.DelFlagEnum;
import cool.webstudy.admin.constant.enums.EnabledStatusFlagEnum;
import cool.webstudy.admin.constant.enums.rescode.NoticeResCodeEnum;
import cool.webstudy.admin.exception.BusinessException;
import cool.webstudy.admin.mapper.notice.NoticeMapper;
import cool.webstudy.admin.model.dto.notice.CreateNoticeDTO;
import cool.webstudy.admin.model.dto.notice.PaginationQueryNoticeInfoListDTO;
import cool.webstudy.admin.model.dto.notice.UpdateNoticeDTO;
import cool.webstudy.admin.model.dto.user.UserDetailInfoDTO;
import cool.webstudy.admin.model.po.notice.NoticePO;
import cool.webstudy.admin.model.vo.notice.PaginationQueryNoticeInfoListVO;
import cool.webstudy.admin.model.vo.notice.QueryNoticeDetailInfoVO;
import cool.webstudy.admin.service.NoticeService;
import cool.webstudy.admin.utils.UserDetailUtil;
import cool.webstudy.common.utils.UUIDUtil;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 公告管理接口实现类
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     *
     * @param unCode 公告业务主键
     * @return TeamPO 公告内容信息实体
     * @description: 判断公告内容是否存在，如果存在，则返回信息实体
     */
    @Override
    public NoticePO isExist(String unCode){
        LambdaQueryWrapper<NoticePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NoticePO::getUnCode,unCode)
                .eq(NoticePO::getMessage, EnabledStatusFlagEnum.ENABLED.getFlag());
        NoticePO notice = noticeMapper.selectOne(queryWrapper);
        if (Objects.nonNull(notice)){
            return notice;
        }
        return  null;
    }

    /**
     * @param unCode 业务主键
     * @return QueryNoticeDetailInfoVO 查询公告内容详细信息接口响应参数模型
     * @description: 查询公告内容详细信息
     */
    @Override
    public QueryNoticeDetailInfoVO queryNoticeDetailInfo(String unCode){
        LambdaQueryWrapper<NoticePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NoticePO::getUnCode,unCode)
                .select(NoticePO::getUnCode,NoticePO::getMessage);
        NoticePO notice = noticeMapper.selectOne(queryWrapper);

        if (Objects.isNull(notice)){
            throw new BusinessException(NoticeResCodeEnum.RC_NOTICE_NOT_EXIST);
        }
        QueryNoticeDetailInfoVO queryNoticeDetailInfoVO= new QueryNoticeDetailInfoVO();
        queryNoticeDetailInfoVO.setUnCode(notice.getUnCode());
        queryNoticeDetailInfoVO.setMessage(notice.getMessage());

        return queryNoticeDetailInfoVO;
    }
    /**
     * @param dto 创建公告内容接口请求参数模型
     * @return Boolean 是否成功标识
     * @description: 创建公告内容
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createNotice(CreateNoticeDTO dto){
        UserDetailInfoDTO userDetailInfo = UserDetailUtil.getUserDetailInfo();
        NoticePO notice = new NoticePO();
        notice.setMessage(dto.getMessage());
        notice.setUnCode(UUIDUtil.getUUID());
        notice.setCreateBy(userDetailInfo.getAccount());
        notice.setCreateTime(new Date());
        notice.setUpdateBy(userDetailInfo.getAccount());
        notice.setUpdateTime(new Date());
        notice.setDelFlag(DelFlagEnum.NOT_DEL_FLAG.getFlag());

        try {
            noticeMapper.insert(notice);
        }catch (Exception e){
            throw new BusinessException(NoticeResCodeEnum.RC_ERROR_CREATE);
        }
        return true;
    }

    /**
     * @param dto 更新公告内容接口请求参数模型
     * @return Boolean 是否成功标识
     * @description: 更新公告内容
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateNotice(UpdateNoticeDTO dto){
        UserDetailInfoDTO userDetailInfo = UserDetailUtil.getUserDetailInfo();
        LambdaUpdateWrapper<NoticePO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NoticePO::getUnCode,dto.getUnCode())
                .set(StringUtils.isNotBlank(dto.getMessage()),NoticePO::getMessage,dto.getMessage())
                .set(NoticePO::getUpdateBy,userDetailInfo.getAccount())
                .set(NoticePO::getUpdateTime, new Date());

        try{
            noticeMapper.update(null,updateWrapper);
        }catch (Exception e){
            throw new BusinessException(NoticeResCodeEnum.RC_ERROR_UPDATE);
        }
        return true;
    }

    /**
     * @param unCodeList 业务主键列表
     * @return Boolean 是否成功标识
     * @description: 批量删除公告内容
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchDeleteNotice(List<String> unCodeList){
        UserDetailInfoDTO userDetailInfo = UserDetailUtil.getUserDetailInfo();
        LambdaUpdateWrapper<NoticePO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(NoticePO::getUnCode,unCodeList)
                .set(NoticePO::getUpdateBy, userDetailInfo.getAccount())
                .set(NoticePO::getUpdateTime,new Date())
                .set(NoticePO::getDelFlag,DelFlagEnum.DEL_FLAG.getFlag());

        try {
            noticeMapper.update(null,updateWrapper);
        }catch (Exception e){
            throw new BusinessException(NoticeResCodeEnum.RC_ERROR_DELETE);
        }
        return  true;
    }

    /**
     * @param dto 分页获取信息列表接口请求参数基础模型
     * @return Page<PaginationQueryTeamInfoListVO> 分页获取公告内容信息列表接口响应模型
     * @description: 分页获取公告内容信息列表
     */
    @Override
    public Page<PaginationQueryNoticeInfoListVO> paginationQueryNoticeInfoList(PaginationQueryNoticeInfoListDTO dto){
        Page<PaginationQueryNoticeInfoListVO> paginationQueryNoticeInfoListVOPage = new Page<>();
        Page<NoticePO> page = new Page<>(dto.getCurrent(), dto.getPageSize());
        LambdaQueryWrapper<NoticePO> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(dto.getContext()),NoticePO::getMessage,dto.getContext());

        Page<NoticePO> noticePOPage = noticeMapper.selectPage(page,queryWrapper);
        if (Objects.isNull(noticePOPage)){
            return paginationQueryNoticeInfoListVOPage;
        }
        List<PaginationQueryNoticeInfoListVO> paginationQueryNoticeInfoListVOList = new ArrayList<>();
        noticePOPage.getRecords().forEach(noticePO -> {
            PaginationQueryNoticeInfoListVO paginationQueryNoticeInfoListVO = new PaginationQueryNoticeInfoListVO();
            paginationQueryNoticeInfoListVO.setUnCode(noticePO.getUnCode());
            paginationQueryNoticeInfoListVO.setMessage(noticePO.getMessage());

            paginationQueryNoticeInfoListVO.setCreateBy(noticePO.getCreateBy());
            paginationQueryNoticeInfoListVO.setCreateTime(noticePO.getCreateTime());
            paginationQueryNoticeInfoListVO.setUpdateBy(noticePO.getUpdateBy());
            paginationQueryNoticeInfoListVO.setUpdateTime(noticePO.getUpdateTime());
            paginationQueryNoticeInfoListVOList.add(paginationQueryNoticeInfoListVO);
        });

        paginationQueryNoticeInfoListVOPage.setRecords(paginationQueryNoticeInfoListVOList);
        paginationQueryNoticeInfoListVOPage.setCurrent(noticePOPage.getCurrent());
        paginationQueryNoticeInfoListVOPage.setPages(noticePOPage.getPages());
        paginationQueryNoticeInfoListVOPage.setSize(noticePOPage.getSize());
        paginationQueryNoticeInfoListVOPage.setTotal(noticePOPage.getTotal());
        return paginationQueryNoticeInfoListVOPage;
    }
}
